import { local } from './utils'
import store from '@/store'
import http from './http'
import { Message } from 'element-ui'
import router from '@/router'
import VueCookies from 'vue-cookies'

// 一天 = 86400秒
const yitain = 86400
// 过期时间  三天
const guoqvDate = 259200

/**
 * @function userLocal
 * 自动登录
 */
function userLocal() {
  const uestinfo = local.get('userinfo_Token')
  if (uestinfo) {
    // store.commit('getuserToken', uestinfo)
    const current_date = Math.ceil(new Date().getTime() / 1000)
    // 判断用户几天内再次打开网页就不刷新token
    if (current_date - uestinfo.past_time < guoqvDate) return
    http.host
      .get('/users/refresh/token', {
        id: uestinfo.userinfo.id,
        email: uestinfo.userinfo.email,
      })
      .then(res => {
        store.commit('getuserToken', res.data[0])
        VueCookies.set('wy_Cookies',res.data[0].cookie,"3d")
      })
      .catch(err => {
        console.error('更新token失败', err)
        Message. warning('请登录')
        // setTimeout(() => {
        //   router.posh({ path: '/login' })
        // }, 500)
      })
  }
}

/**
 * @function Vuex_userinfo
 * 处理网页刷新Vuex中的用户数据丢失
 */
function Vuex_userinfo() {
  const uestinfo = local.get('userinfo_Token')
  if (uestinfo) {
    store.commit('getuserToken', uestinfo)
  }
}

export default { userLocal, Vuex_userinfo }
